package com.fingard.dsp.bank.directbank.bob01;

import java.util.Date;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.PaymentState;
import com.fingard.dsp.bank.directbank.bankInterface.IBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.ReqBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.ReqQryBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.RetAtsBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.RetAtsQryBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.detail.RetAtsBatPayRecDetail;
import com.fingard.net.ConnState;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

public class BOB01BatPayRec extends BOB01Base implements IBatPayRec {
	//1988
	public RetAtsBatPayRec d_1988_BatPay(ReqBatPayRec p_req1988) throws Exception{
		return d_BatPayRec(p_req1988);
	}
	//9188
	public RetAtsBatPayRec d_9188_BatPay(ReqBatPayRec p_req9188) throws Exception{
		return d_BatPayRec(p_req9188);
	}
	
	//批量代收和批量代付提交
	public RetAtsBatPayRec d_BatPayRec(ReqBatPayRec p_reqBat) throws Exception{
		//返回ATS的对象
		RetAtsBatPayRec retAts = new RetAtsBatPayRec(p_reqBat.reqHeader);
		try {

			XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
			writeCommonHeader(xmlWriter);
			xmlWriter.writeStartElement("opReq");
			String opName = null;
			//CebankGZBatchPayOp:代发工资;  CebankDKBatchPayOp:代扣
			if(p_reqBat.reqHeader.transType.equals("1988")){
				opName = "CebankGZBatchPayNewOp";
			}
			else if(p_reqBat.reqHeader.transType.equals("9188")){
				opName = "CebankDKBatchPayNewOp";
			}
			
			xmlWriter.writeElementString("opName", opName);
			xmlWriter.writeElementString("serialNo", p_reqBat.reqBatchNO);
			xmlWriter.writeElementString("reqTime", Format.Date8Format.format(new Date()));
			xmlWriter.writeStartElement("ReqParam");
			if(p_reqBat.reqHeader.transType.equals("1988")){
				xmlWriter.writeElementString("paySalaryNo", getBankConfig().getMapValueByKey("busi_no_Mapping", p_reqBat.ownAct));
				xmlWriter.writeElementString("payDepAcc", p_reqBat.ownAct);
			}
			else if(p_reqBat.reqHeader.transType.equals("9188")){
				xmlWriter.writeElementString("receiveNum", getBankConfig().getMapValueByKey("busi_no_Mapping", p_reqBat.ownAct));
				xmlWriter.writeElementString("recDepAcc", p_reqBat.ownAct);
			}
			
			xmlWriter.writeElementString("precontractTime", Format.Date8Format.format(Format.Date10Format.parse(p_reqBat.payDate)));
			xmlWriter.writeElementString("totalNum", p_reqBat.totalNum);
			xmlWriter.writeElementString("totalAmt", p_reqBat.totalAmt.replaceAll("\\.", ""));
			xmlWriter.writeElementString("currencyType", this.convertCurToBank(p_reqBat.ownCur));
			xmlWriter.writeElementString("bankBookSummary", getBankConfig().getMapValueByKey("usageMapping", p_reqBat.useType));
			xmlWriter.writeElementString("tranMode", "0");
			xmlWriter.writeElementString("detainSubAmt", "N");

			xmlWriter.writeElementClosed("reqReserved1");
			xmlWriter.writeElementClosed("reqReserved2");
			xmlWriter.writeStartElement("opReqSet");
			
			String memo = p_reqBat.useType;
			if(memo.length()>20){
				memo = memo.substring(0,21);
			}
			memo=memo+"#"+p_reqBat.abstractATS;						
			//需要进行对账码拼接以及节点解析，采用XElement进行处理
			int tmpCount = 0;
			while(p_reqBat.readRecord()){
				tmpCount++;
				xmlWriter.writeStartElement("opRequest");
				xmlWriter.writeElementString("orderNo", tmpCount+"");

				if(p_reqBat.reqHeader.transType.equals("1988")){//工资
					xmlWriter.writeElementString("recPerAcc", p_reqBat.readedDetail.recAct);
					xmlWriter.writeElementString("recPerName", p_reqBat.readedDetail.recName);
				}
				else{
					xmlWriter.writeElementString("payPerAcc", p_reqBat.readedDetail.payAct);
					xmlWriter.writeElementString("payPerName", p_reqBat.readedDetail.payName);					
				}
				
				if(StringHelper.isNullOrEmpty(p_reqBat.readedDetail.certType)){
					xmlWriter.writeElementClosed("certType");
					xmlWriter.writeElementClosed("certNum");
				}else{
					xmlWriter.writeElementString("certType", this.convertCertTypeToBank(p_reqBat.readedDetail.certType));
					xmlWriter.writeElementString("certNum", p_reqBat.readedDetail.certNum);
				}
				
				xmlWriter.writeElementString("payAmt", p_reqBat.readedDetail.payAmount.replaceAll("\\.", ""));
				
				if(p_reqBat.reqHeader.transType.equals("9188")){//工资
					xmlWriter.writeElementString("payTypeID", "01");
					xmlWriter.writeElementString("payTypeName", p_reqBat.readedDetail.payType);
				}
				xmlWriter.writeElementString("userRem", memo);
				xmlWriter.writeElementString("reqReserved3", p_reqBat.readedDetail.askNumber);
				xmlWriter.writeElementClosed("reqReserved4");
				xmlWriter.writeEndElement();
				
			}
			xmlWriter.writeEndElement();
			xmlWriter.writeEndElement();
			xmlWriter.writeEndElement();
			xmlWriter.writeEndDocument();//写入报文完成
			
			String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(),opName);//发送银行
			if(tmpStrRet[0].length() == 0){
				StringBuilder tmpSbMsg = new StringBuilder();
				//解析银行返回
				XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
				if(tmpBkReader != null){
					retAts.respCode = "0";
					retAts.respInfo = tmpBkReader.getFirstText("/BCCBEBankData/opRep/errMsg");
					retAts.reqSeqID = p_reqBat.reqBatchNO;
					retAts.transState = tmpBkReader.getFirstText("/BCCBEBankData/opRep/retCode");
					retAts.payInfoCode = retAts.transState;
				}
				else{
					retAts.respCode = "-1";
					retAts.respInfo = tmpSbMsg.toString();
				}
			}
			else if(tmpStrRet[0].equals(ConnState.FAIL)){
				retAts.respCode = "0";
				retAts.respInfo = tmpStrRet[1];
				retAts.reqSeqID = p_reqBat.reqBatchNO;
				retAts.transState = PaymentState.FAIL_Batch;
				retAts.payInfoCode = tmpStrRet[0];
			}
			else{
				retAts.respCode = "-1";
				retAts.respInfo = tmpStrRet[1];
			}
			
		}catch (Exception e) {
			retAts.respCode = "-1";
			retAts.respInfo = e.toString();
			e.printStackTrace();
		}
		if(retAts.respCode.equals("-1") && StringHelper.isNullOrEmpty(retAts.reqSeqID)){
			retAts.reqSeqID = p_reqBat.reqBatchNO;
		}
		retAts.writeRetToATS();
		return retAts;
	}

	//1998 状态查询
	public RetAtsQryBatPayRec d_1998_QryBatPay(ReqQryBatPayRec p_req1998) throws Exception{
		return d_QryBatPayRec(p_req1998);
	}
	//9198
	public RetAtsQryBatPayRec d_9198_QryBatPay(ReqQryBatPayRec p_req9198) throws Exception{
		return d_QryBatPayRec(p_req9198);
	}
	
	//批量代收和批量代付状态查询
	public RetAtsQryBatPayRec d_QryBatPayRec(ReqQryBatPayRec p_reqQryBat) throws Exception{
		//返回ATS的对象
		RetAtsQryBatPayRec retAts = new RetAtsQryBatPayRec(p_reqQryBat.reqHeader);
		retAts.respCode = "";

		XmlTextWriter xmlWriter = null;
		xmlWriter = new XmlTextWriter();//发银行报文的XML对象
		writeCommonHeader(xmlWriter);//写入通用的头部节点
		xmlWriter.writeStartElement("opReq");
		String opName = null;
		//CebankGZBatchPayOp:代发工资;  CebankDKBatchPayOp:代扣
		if(p_reqQryBat.reqHeader.transType.equals("1998")){
			opName = "CebankQryGZBatchDetailNewOp";
		}
		else if(p_reqQryBat.reqHeader.transType.equals("9198")){
			opName = "CebankQryDKBatchDetailNewOp";
		}
		
		xmlWriter.writeElementString("opName", opName);
		xmlWriter.writeElementString("serialNo", getQryTransID());
		xmlWriter.writeElementString("reqTime", Format.Date8Format.format(new Date()));
		xmlWriter.writeStartElement("ReqParam");
		xmlWriter.writeElementString("batchSerialNo", p_reqQryBat.reqBatchNO);
		xmlWriter.writeEndElement();
		xmlWriter.writeEndElement();
		xmlWriter.writeEndDocument();
		
		try{
			
			String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(),opName);
			if(tmpStrRet[0].length() == 0){
				StringBuilder tmpSbMsg = new StringBuilder();
				XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
				if(tmpBkReader != null){
					String tmpStatus = tmpBkReader.getFirstText("/BCCBEBankData/opRep/retCode");
					String tmpStatusText = tmpBkReader.getFirstText("/BCCBEBankData/opRep/errMsg");
					if(tmpStatus != null && tmpStatus.length() > 0){
						retAts.respCode = "0";
						retAts.reqSeqID = p_reqQryBat.reqBatchNO;
						int tmpCountRow = tmpBkReader.countElement("/BCCBEBankData/opRep/opResultSet/opResult");
						if(tmpCountRow>0 && isActiveRetCode(tmpStatus)){
							retAts.writeBeforeDetail();
							while(tmpBkReader.read("/BCCBEBankData/opRep/opResultSet/opResult")){
								RetAtsBatPayRecDetail tmpRetDetail = new RetAtsBatPayRecDetail();
								if(p_reqQryBat.reqHeader.transType.equals("1998")){
									tmpRetDetail.recAct = tmpBkReader.readedXElement.getSingleText("recPerAcc");
									tmpRetDetail.recName = tmpBkReader.readedXElement.getSingleText("recPerName");
								}
								else if(p_reqQryBat.reqHeader.transType.equals("9198")){
									tmpRetDetail.payAct = tmpBkReader.readedXElement.getSingleText("payPerAcc");
									tmpRetDetail.payName = tmpBkReader.readedXElement.getSingleText("payPerName");
								}
								tmpRetDetail.payAmount = tmpBkReader.readedXElement.getSingleText("payAmt");
								tmpRetDetail.postScript = tmpBkReader.readedXElement.getSingleText("reqReserved3");
								tmpRetDetail.result = tmpBkReader.readedXElement.getSingleText("result");
								tmpRetDetail.memo = tmpBkReader.readedXElement.getSingleText("bankRem");
								tmpRetDetail.payInfoCode = tmpRetDetail.result;
	                            
								retAts.writeRetDetail(tmpRetDetail);
							}
						}
						else if(tmpCountRow == 0){
							retAts.transState = tmpStatus+ "+" +tmpStatusText;
							retAts.respInfo = tmpStatusText;
							retAts.payInfoCode = tmpStatus;
							retAts.writeBeforeDetail();
						}
						else{
							retAts.respCode = "-1";
							retAts.respInfo = tmpStatusText;
						}
					}
					else{
						retAts.respCode = "-1";
						retAts.respInfo = tmpStatusText;
					}
				}
				else{
					retAts.respCode = "-1";
					retAts.respInfo = tmpSbMsg.toString();
				}
			}
			else{
				retAts.respCode = "-1";
				retAts.respInfo = tmpStrRet[1];
			}
		}
		catch (Exception e) {
			retAts.respCode = "-1";
			retAts.respInfo = e.getMessage();
			e.printStackTrace();
		}
		if(retAts.respCode.equals("-1") && StringHelper.isNullOrEmpty(retAts.reqSeqID)){
			retAts.reqSeqID = p_reqQryBat.reqBatchNO;
		}
		if(retAts.respCode.equals("-1")){
			retAts.writeBeforeDetail();
		}
		retAts.writeAfterDetail();
		return retAts;
	}	
}
